package com.sq.swing.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sq.swing.framework.common.ResourceLocator;

public class XMLUtils {

	private Map<String, String> xmlMap = new HashMap<String, String>();

	public static final String XML_NODE_NAME_SCREEN = "screen";
	public static final String XML_NODE_NAME_TESTKEY42 = "testKey42";
	public static final String XML_NODE_NAME_TESTKEY2 = "testKey2";
	public static final String XML_NODE_NAME_TESTKEY1 = "testKey1";
	public static final String XML_NODE_NAME_SEARCHLABEL = "searchLabel";

	public XMLUtils(String filePath) throws Exception {
		init(filePath);
	}

	/**
	 *
	 */
	public void init(String filePath) throws Exception {
		Document document = parse(filePath);
		Element root = document.getDocumentElement();
		foreachXmlToMap(root, root);
	}

	public String getValueFromXml(String[] xmlNodeNames) {
		return xmlMap.get(buildXmlkey(xmlNodeNames));
	}

	private static String buildXmlkey(String[] xmlNodeNames) {
		if (xmlNodeNames == null) {
			return null;
		}

		String xmlKeys = "|";
		for (String str : xmlNodeNames) {
			xmlKeys += str + "|";
		}

		return xmlKeys;
	}

	private Document parse(String filePath) throws Exception{
		Document document = null;
		try {
			DocumentBuilderFactory builderFactory = DocumentBuilderFactory
					.newInstance();
			// DOM parser instance
			DocumentBuilder builder = builderFactory.newDocumentBuilder();
			// parse an XML file into a DOM tree
			document = builder.parse(new File(ResourceLocator
					.loadFile(filePath)));
		} catch (Exception e) {
			throw e;
		}
		return document;
	}

	private void foreachXmlToMap(Node rootNode, Element e) {
		if (e != null) {

			if (e.getChildNodes() != null) {
				NodeList nodeList = e.getChildNodes();
				for (int i = 0; i < nodeList.getLength(); i++) {

					Node node = nodeList.item(i);
					if (node.getChildNodes().getLength() > 0) {
						foreachXmlToMap(rootNode, (Element) node);
					} else {
						if (node.getNodeType() == Node.TEXT_NODE) {
							xmlMap.put(getParentElements(rootNode, node, null),
									node.getNodeValue());
						}
					}
				}
			} else {
			}
		}
	}

	private static String getParentElements(Node rootNode, Node node,
			StringBuilder parentElements) {

		if (parentElements == null) {
			parentElements = new StringBuilder();
		}

		if (node.getParentNode() == null) {
			if (node.getNodeType() != Node.DOCUMENT_NODE) {
				// parentElements.append(node.getNodeName());
			}

		} else if (node.getParentNode().isEqualNode(rootNode)) {
			parentElements.append(node.getNodeName() + "|");
			getParentElements(rootNode, node.getParentNode(), parentElements);
		} else {
			if (node.getNodeType() == Node.TEXT_NODE) {
				parentElements.append("|");
			} else {
				parentElements.append(node.getNodeName() + "|");
			}
			getParentElements(rootNode, node.getParentNode(), parentElements);
		}
		return parentElements.toString();
	}

}